home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / FunctionCode.c < prev    next >
Text File  |  1994-08-15  |  5KB  |  140 lines

  1. /* FunctionCode.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "FunctionCode.h"
  31. #include "Memory.h"
  32. #include "SymbolList.h"
  33. #include "SymbolTableEntry.h"
  34.  
  35.  
  36. struct FuncCodeRec
  37.     {
  38.         char*                            FunctionName;
  39.         long                            NumParameters;
  40.         DataTypes*                ParameterTypeList;
  41.         PcodeRec*                    Code;
  42.         DataTypes                    ReturnType;
  43.     };
  44.  
  45.  
  46. /* dispose of a function object */
  47. void                            DisposeFunction(FuncCodeRec* Function)
  48.     {
  49.         CheckPtrExistence(Function);
  50.         ReleasePtr(Function->FunctionName);
  51.         ReleasePtr((char*)Function->ParameterTypeList);
  52.         DisposePcode(Function->Code);
  53.         ReleasePtr((char*)Function);
  54.     }
  55.  
  56.  
  57. /* get the actual code block for a function. */
  58. struct PcodeRec*    GetFunctionPcode(FuncCodeRec* FuncCode)
  59.     {
  60.         CheckPtrExistence(FuncCode);
  61.         return FuncCode->Code;
  62.     }
  63.  
  64.  
  65. /* get the name of a function (actual, not copy).  it's a pointer into the heap */
  66. char*                            GetFunctionName(FuncCodeRec* FuncCode)
  67.     {
  68.         CheckPtrExistence(FuncCode);
  69.         return FuncCode->FunctionName;
  70.     }
  71.  
  72.  
  73. /* get the list of parameters for a function.  it returns a heap block array */
  74. /* of data types with fields from left parameter to right parameter. */
  75. DataTypes*                GetFunctionParameterTypeList(FuncCodeRec* FuncCode)
  76.     {
  77.         CheckPtrExistence(FuncCode);
  78.         return FuncCode->ParameterTypeList;
  79.     }
  80.  
  81.  
  82. /* get the data type a function returns.  could be undefined. */
  83. DataTypes                    GetFunctionReturnType(FuncCodeRec* FuncCode)
  84.     {
  85.         CheckPtrExistence(FuncCode);
  86.         return FuncCode->ReturnType;
  87.     }
  88.  
  89.  
  90. /* create a new function thing */
  91. FuncCodeRec*            NewFunction(char* FuncName, long NameLength,
  92.                                         struct SymbolListRec* Parameters, struct PcodeRec* PcodeThing,
  93.                                         DataTypes ReturnType)
  94.     {
  95.         FuncCodeRec*        FuncCode;
  96.         SymbolListRec*    FormalParameterScanner;
  97.         long                        ParamIndex;
  98.  
  99.         FuncCode = (FuncCodeRec*)AllocPtrCanFail(sizeof(FuncCodeRec),"FuncCodeRec");
  100.         if (FuncCode == NIL)
  101.             {
  102.              FailurePoint1:
  103.                 return NIL;
  104.             }
  105.  
  106.         FuncCode->FunctionName = AllocPtrCanFail(NameLength,"FuncCodeRec:name");
  107.         if (FuncCode->FunctionName == NIL)
  108.             {
  109.              FailurePoint2:
  110.                 ReleasePtr((char*)FuncCode);
  111.                 goto FailurePoint1;
  112.             }
  113.         CopyData(FuncName,FuncCode->FunctionName,NameLength);
  114.  
  115.         FuncCode->NumParameters = GetSymbolListLength(Parameters);
  116.         FuncCode->ParameterTypeList = (DataTypes*)AllocPtrCanFail(sizeof(DataTypes)
  117.             * FuncCode->NumParameters,"FuncCodeRec:ParameterTypeList");
  118.         if (FuncCode->ParameterTypeList == NIL)
  119.             {
  120.              FailurePoint3:
  121.                 ReleasePtr((char*)FuncCode->FunctionName);
  122.                 goto FailurePoint2;
  123.             }
  124.         FormalParameterScanner = Parameters;
  125.         ParamIndex = 0;
  126.         while (FormalParameterScanner != NIL)
  127.             {
  128.                 FuncCode->ParameterTypeList[ParamIndex] = GetSymbolVariableDataType(
  129.                     GetFirstFromSymbolList(FormalParameterScanner));
  130.                 FormalParameterScanner = GetRestListFromSymbolList(FormalParameterScanner);
  131.                 ParamIndex += 1;
  132.             }
  133.  
  134.         FuncCode->Code = PcodeThing;
  135.  
  136.         FuncCode->ReturnType = ReturnType;
  137.  
  138.         return FuncCode;
  139.     }
  140.